<?php
require_once("./include/db_info.inc.php");
require_once("./bm-header.php");
require_once("./include/funcs.php");
$TeamBool = 0;//是否已注册
$is_admin = false;
if (isset($_GET['cid']))
{
	$cid=intval($_GET['cid']);
	$view_cid=$cid;
	// check contest valid
	$sql="SELECT * FROM `bm_contest` WHERE `bm_c_id`='$cid' ";
	$result=mysql_query($sql);
	$rows_cnt=mysql_num_rows($result);
	$contest_ok=true;
	if ($rows_cnt==0)
	{
		mysql_free_result($result);
		echo "<h2>No Such Contest!</h2><br/><br/>";
		echo '<img src="./images/404.jpg">';
		require_once("bm-footer.php");
		exit(0);
	}
	else
	{
		$row=mysql_fetch_object($result);
		$now=time();
		$Ckind=$row->bm_c_kind;
		$SRtime=strtotime($row->bm_c_startreg);
		$ERtime=strtotime($row->bm_c_endreg);
		$bm_c_defunct=$row->bm_c_defunct;
		$view_description=$row->bm_c_description;
		$view_title= $row->bm_c_title;
		$view_start_time=$row->bm_c_start;
		$view_end_time=$row->bm_c_end;
		$view_SRtime=$row->bm_c_startreg;
		$view_ERtime=$row->bm_c_endreg;
		mysql_free_result($result);
		$bm_c_id=$cid;
		$bm_t_ojid=$_SESSION['user_id'];
		$sql="SELECT * FROM `bm_team` WHERE `bm_c_id`=$bm_c_id AND `bm_t_ojid`='$bm_t_ojid'";
		$result=mysql_query($sql);
		global $TeamBool;
		$TeamBool=mysql_num_rows($result);
		if ($row->bm_c_defunct=='Y' || !isset($_SESSION['user_id'])
			|| $now < $SRtime || !$TeamBool) $contest_ok=false;
		if (isset($_SESSION[$BM_ADMIN]) || isset($_SESSION['administrator']))
		{
			$contest_ok=true;
			$is_admin=true;
		}
	}
	if (!$contest_ok && $bm_c_defunct=='Y')
	{
		mysql_free_result($result);
		echo "<h2>No Such Contest!</h2><br/><br/>";
		echo '<img src="./images/404.jpg">';
		require_once("bm-footer.php");
		exit(0);
	}
	if (!$contest_ok)
	{
		echo "<h2>No Permission To See The Result.</h2><br/><br/>";
		echo '<img src="./images/404.jpg">';
		require_once("bm-footer.php");
		exit(0);
	}
?>
	<center><h3>Contest<?php echo $view_cid;?> - <?php echo $view_title;echo "(".($Ckind=='0'?"组队赛":"个人赛").")"; ?> 报名信息</h3></center> 
    <div id="results">
	<?php
	$cnt = 0;
	echo "<table id='show_results' name='show_results'>";
	if($Ckind==1)//个人赛
	{
		$sql="
		SELECT * FROM `bm_team`, `bm_stu` 
		WHERE `bm_team`.`bm_t_id`=`bm_stu`.`bm_t_id` AND `bm_team`.`bm_c_id`='$bm_c_id' AND `bm_s_123`=0
		ORDER BY `bm_team`.`bm_t_id` ASC
		";
		$result=mysql_query($sql);
		if($is_admin)
		{
			echo "<tr><td><nobr>序号</nobr></td><td><nobr>学号</nobr></td><td><nobr>姓名</nobr></td><td><nobr>学校/学院</nobr></td><td><nobr>班级</nobr></td><td><nobr>性别</nobr></td><td><nobr>电话</nobr></td><td><nobr>邮箱</nobr></td><td><nobr>使用编程语言</nobr></td><td><nobr>指导老师</nobr></td><td><nobr>OnlineJudge_ID</nobr></td><td><nobr>删除报名</nobr></td></tr>";
		}
		else
		{
			echo "<tr><td><nobr>序号</nobr></td><td><nobr>学号</nobr></td><td><nobr>姓名</nobr></td><td><nobr>学院/学校</nobr></td><td><nobr>专业班级</nobr></td><td><nobr>性别</nobr></td><td><nobr>使用编程语言</nobr></td><td><nobr>指导老师</nobr></td></tr>";
		}
		while($row = mysql_fetch_array($result))
		{
			$cnt ++;
			$bm_t_id = SqlToHtml($row['bm_t_id']);
			$bm_s_number = SqlToHtml($row['bm_s_number']);
			$bm_s_name = SqlToHtml($row['bm_s_name']);
			$bm_s_school = SqlToHtml($row['bm_s_school']);
			$bm_s_class = SqlToHtml($row['bm_s_class']);
			$bm_s_phone = SqlToHtml($row['bm_s_phone']);
			$bm_s_mail = SqlToHtml($row['bm_s_mail']);
			$bm_t_coach = SqlToHtml($row['bm_t_coach']);
			$bm_t_ojid = SqlToHtml($row['bm_t_ojid']);
			$bm_s_sex = ($row['bm_s_sex']);
			$bm_s_codelang = ($row['bm_s_codelang']);
			if($is_admin)
			{
				echo "<tr><td><nobr>{$cnt}</nobr></td><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($row['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_phone</nobr></td><td><nobr>$bm_s_mail</nobr></td><td><nobr>$bm_s_codelang</nobr></td><td><nobr>$bm_t_coach</nobr></td><td><nobr><a href = '".$OJ_HOME."/userinfo.php?user=".$bm_t_ojid."'>$bm_t_ojid</nobr></td><td><nobr><a href='./admin/info_delete.php?tid=$bm_t_id&cid=$bm_c_id&ojid=$bm_t_ojid&oper=0&getkey=".$_SESSION['getkey']."'>删除</a></nobr></td></tr>";
			}
			else
			{
				echo "<tr><td><nobr>{$cnt}</nobr></td><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($row['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_codelang</nobr></td><td><nobr>$bm_t_coach</nobr></td></tr>";
			}
		}
		mysql_free_result($result);
	}
	else//组队赛
	{
		$sql="
		SELECT * FROM `bm_team`
		WHERE `bm_c_id`='$bm_c_id'
		ORDER BY `bm_t_id` ASC
		";
		$result=mysql_query($sql);
		if($is_admin)
		{
			echo "<tr><td><nobr>序号</nobr></td><td><nobr>队伍名称</nobr></td><td><nobr>学号</nobr></td><td><nobr>姓名</nobr></td><td><nobr>学院/学校</nobr></td><td><nobr>专业班级</nobr></td><td><nobr>性别</nobr></td><td><nobr>电话</nobr></td><td><nobr>邮箱</nobr></td><td><nobr>使用编程语言</nobr></td><td><nobr>指导老师</nobr></td><td><nobr>OnlineJudge_ID</nobr></td><td><nobr>删除报名</nobr></td></tr>";
		}
		else
		{
			echo "<tr><td><nobr>序号</nobr></td><td><nobr>队伍名称</nobr></td><td><nobr>学号</nobr></td><td><nobr>姓名</nobr></td><td><nobr>学院/学校</nobr></td><td><nobr>专业班级</nobr></td><td><nobr>性别</nobr></td><td><nobr>使用编程语言</nobr></td><td><nobr>指导老师</nobr></td></tr>";
		}
		while($row = mysql_fetch_array($result))
		{
			$bm_t_id = $row['bm_t_id'];
			$bm_t_coach = SqlToHtml($row['bm_t_coach']);
			$bm_t_ojid = SqlToHtml($row['bm_t_ojid']);
			$bm_t_name = SqlToHtml($row['bm_t_name']);
			$sql="
			SELECT * FROM `bm_stu` 
			WHERE `bm_t_id`='$bm_t_id'
			ORDER BY `bm_s_123` ASC
			";
			$cnt ++;
			$lcnt = 0;
			$lres = mysql_query($sql);
			while($lrow = mysql_fetch_array($lres))
			{
				$lcnt ++;
				if($lcnt > 3) break;
				$bm_t_id = SqlToHtml($row['bm_t_id']);
				$bm_s_number = SqlToHtml($lrow['bm_s_number']);
				$bm_s_name = SqlToHtml($lrow['bm_s_name']);
				$bm_s_school = SqlToHtml($lrow['bm_s_school']);
				$bm_s_class = SqlToHtml($lrow['bm_s_class']);
				$bm_s_phone = SqlToHtml($lrow['bm_s_phone']);
				$bm_s_mail = SqlToHtml($lrow['bm_s_mail']);
				$bm_s_sex = ($lrow['bm_s_sex']);
				$bm_s_codelang = ($lrow['bm_s_codelang']);
				if($is_admin)
				{
					if($lcnt == 1)
						echo "<tr><td rowspan = '3'>{$cnt}</nobr></td><td rowspan = '3'>$bm_t_name</nobr></td><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($lrow['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_phone</nobr></td><td><nobr>$bm_s_mail</nobr></td><td><nobr>$bm_s_codelang</nobr></td><td rowspan = '3'>$bm_t_coach</nobr></td><td rowspan = '3'>$bm_t_ojid</nobr></td><td rowspan = '3'><a href='./admin/info_delete.php?tid=$bm_t_id&cid=$bm_c_id&ojid=$bm_t_ojid&oper=0&getkey=".$_SESSION['getkey']."'>删除</a></nobr></td></tr>";
					else
						echo "<tr><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($lrow['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_phone</nobr></td><td><nobr>$bm_s_mail</nobr></td><td><nobr>$bm_s_codelang</nobr></td></tr>";
						
				}
				else
				{
					if($lcnt == 1)
						echo "<tr><td rowspan = '3'>{$cnt}</nobr></td><td rowspan = '3'>$bm_t_name</nobr></td><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($lrow['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_codelang</nobr></td><td rowspan = '3'>$bm_t_coach</nobr></td></tr>";
					else
						echo "<tr><td><nobr>$bm_s_number</nobr></td><td><nobr>$bm_s_name</nobr></td><td><nobr>$bm_s_school</nobr></td><td><nobr>$bm_s_class</nobr></td><td><nobr>".($lrow['bm_s_sex']?"女":"男")."</nobr></td><td><nobr>$bm_s_codelang</nobr></td></tr>";
				}
			}
		}
	}
	echo "</table>";
}
?>
	</div>
<?php
require_once("bm-footer.php");
?>